#ifndef UHM_MAT_EXTENSION_CHOL_PIV_HXX_
#define UHM_MAT_EXTENSION_CHOL_PIV_HXX_

namespace uhm {
  
  // Basic CHOL piv...
  template<class A_, class B_>
  class CHOL_piv_ : public CHOL_nopiv_<A_,B_> {
  private:
  protected:
  public:

    CHOL_piv_() : CHOL_nopiv_<A_,B_>() { }
    CHOL_piv_(Int_ type, Int_ uplo, Int_ sym,
              Int_ mt, Int_ mb,
              Int_ rhs, Int_ blk) 
      : CHOL_nopiv_<A_,B_>(type, mt, mb, rhs, blk) { }
    CHOL_piv_(Int_ type, 
              Int_ mt, Int_ mb, 
              Int_ rhs, 
              Int_ blk)  
      : CHOL_nopiv_<A_,B_>(type, mt, mb, rhs, blk) { }
    
    CHOL_piv_(const CHOL_piv_ &b) : CHOL_nopiv_<A_,B_>(b) { }
    
    using CHOL_nopiv_<A_,B_>::get_leading_flop;
	  
    using CHOL_nopiv_<A_,B_>::decompose;
    using CHOL_nopiv_<A_,B_>::solve_1_x;
    using CHOL_nopiv_<A_,B_>::solve_2_x;

    virtual Int_ update() {

      if (this->get_mt() && this->get_mb()) {
        apply_pivots( Mat_Base_::RIGHT, Mat_Base_::TRANSPOSE, 
                      this->_P, this->_ABL );
        trsm( Mat_Base_::RIGHT, Mat_Base_::LOWER_TRIANGULAR, 
              Mat_Base_::CONJ_TRANSPOSE, Mat_Base_::NONUNIT_DIAG,
              1, this->_ATL, this->_ABL );
        herk( Mat_Base_::LOWER_TRIANGULAR, Mat_Base_::NO_TRANSPOSE, 
              -1, this->_ABL, 1, this->_ABR );
      }
      return true;
    }
    
    virtual Int_ decompose() {
      if (this->get_mt()) 
        chol_piv( Mat_Base_::LOWER_TRIANGULAR, this->_ATL, this->_P );

      this->update();

      return true;
    }

    virtual Int_ solve_1_x() {
      if (this->get_mt()) {
        apply_pivots( Mat_Base_::LEFT, Mat_Base_::NO_TRANSPOSE,
                      this->_P, this->_XT );
        trsm( Mat_Base_::LEFT, Mat_Base_::LOWER_TRIANGULAR,
              Mat_Base_::NO_TRANSPOSE, Mat_Base_::NONUNIT_DIAG,
              1, this->_ATL, this->_XT );
      }
      if (this->get_mt() && this->get_mb()) {
        gemm( Mat_Base_::NO_TRANSPOSE, Mat_Base_::NO_TRANSPOSE,
              -1, this->_ABL, this->_XT, 1, this->_XB );
      }
      return true;
    }

    virtual Int_ solve_2_x() {
      if (this->get_mb() && this->get_mt()) {
        gemm( Mat_Base_::CONJ_TRANSPOSE, Mat_Base_::NO_TRANSPOSE,
              -1, this->_ABL, this->_XB, 1, this->_XT );
      }

      if (this->get_mt()) {
        trsm( Mat_Base_::LEFT, Mat_Base_::LOWER_TRIANGULAR,
              Mat_Base_::CONJ_TRANSPOSE, Mat_Base_::NONUNIT_DIAG, 
              1, this->_ATL,  this->_XT );
        apply_pivots( Mat_Base_::LEFT, Mat_Base_::TRANSPOSE,
                      this->_P, this->_XT );
      }
      return true;
    }

    virtual Int_ solve_1_r() {
      if (this->get_mt()) {
        apply_pivots( Mat_Base_::LEFT, Mat_Base_::NO_TRANSPOSE,
                      this->_P, this->_RT );
        trsm( Mat_Base_::LEFT, Mat_Base_::LOWER_TRIANGULAR,
              Mat_Base_::NO_TRANSPOSE, Mat_Base_::NONUNIT_DIAG,
              1, this->_ATL, this->_RT );
      }
      if (this->get_mt() && this->get_mb()) {
        gemm( Mat_Base_::NO_TRANSPOSE, Mat_Base_::NO_TRANSPOSE,
              -1, this->_ABL, this->_RT, 1, this->_RB );
      }
      return true;
    }

    virtual Int_ solve_2_r() {
      if (this->get_mb() && this->get_mt()) {
        gemm( Mat_Base_::CONJ_TRANSPOSE, Mat_Base_::NO_TRANSPOSE,
              -1, this->_ABL, this->_RB, 1, this->_RT );
      }

      if (this->get_mt()) {
        trsm( Mat_Base_::LEFT, Mat_Base_::LOWER_TRIANGULAR,
              Mat_Base_::CONJ_TRANSPOSE, Mat_Base_::NONUNIT_DIAG, 
              1, this->_ATL,  this->_RT );
        apply_pivots( Mat_Base_::LEFT, Mat_Base_::TRANSPOSE,
                      this->_P, this->_RT );
      }
      return true;
    }
  };

}

#endif
